package com.zyh.system.JDsupermarkt.service.impl;

import com.zyh.system.JDsupermarkt.dao.impl.GoodsDaoImpl;
import com.zyh.system.JDsupermarkt.domain.Goods;
import com.zyh.system.JDsupermarkt.service.GoodsService;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Scanner;
import java.util.Set;

/**
 * @Author zyh
 * @Date 2024/8/2 17:10
 * @Description GoodsService 实现类
 */

public class GoodsServiceImpl implements GoodsService {
    public static final Scanner INPUT = new Scanner(System.in);
    public static final GoodsDaoImpl GOODS_DAO = new GoodsDaoImpl();


    /**
     * 添加商品
     *
     * @param map Goods集合
     */
    @Override
    public void addGoods(LinkedHashMap<Integer, Goods> map) throws IOException {
        Integer goodsId;
        while (true) {
            System.out.println("请输入商品编号：");
            goodsId = INPUT.nextInt();
            //假设用户输入的编号不存在
            boolean flag = false;
            Set<Integer> keySet = map.keySet();
            for (Integer key : keySet) {
                if (key.equals(goodsId)) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                //如果存在，则进行修改的操作
                System.out.println("您输入的商品编号已经存在。\n信息如下：");
                //输出存在的对应商品信息
                System.out.println("编号\t商品名称\t\t\t价格\t颜色\t尺寸\t库存");
                for (Integer key : map.keySet()) {
                    Goods value = map.get(key);
                    if (key.equals(goodsId)) {
                        System.out.println(key + "\t" + value.getGoodsName() + "\t\t\t" + value.getGoodsPrice() + "\t" + value.getGoodsColor() + "\t" + value.getGoodsSize() + "\t" + value.getStock());
                    }
                }
                System.out.println("是否进行修改（y/n）");
                String confirm = INPUT.next();
                if ("y".equals(confirm)) {
//                    updateById(map);
                    update(map, goodsId);
                    return;
                } else {
                    //不修改就直接结束方法
                    return;
                }
            } else {
                //不存在就新添加商品信息
                System.out.println("请输入商品名称：");
                String goodsName = INPUT.next();
                Double goodsPrice;
                //价格数据校验
                while (true) {
                    System.out.println("请输入商品价格：");
                    goodsPrice = INPUT.nextDouble();
                    if (goodsPrice < 0) {
                        System.out.println("价格必须大于0");
//                        continue;
                    } else {
                        break;
                    }
                }
                System.out.println("请输入商品颜色：");
                String goodsColor = INPUT.next();
                System.out.println("请输入商品尺寸：");
                Double goodsSize = INPUT.nextDouble();
                Integer goodsStock;
                //库存数据校验
                while (true) {
                    System.out.println("请输入商品库存：");
                    goodsStock = INPUT.nextInt();
                    if (goodsStock < 0) {
                        System.out.println("商品库存必须大于0");
//                        continue;
                    } else {
                        break;
                    }
                }
                //拿到所有正确的信息封装成Goods对象
                Goods goods = new Goods(goodsName, goodsPrice, goodsColor, goodsSize, goodsStock);
                //调用Dao层方法，根据返回值确认是否添加成功
                boolean is_successful = GOODS_DAO.addGoods(map, goods, goodsId);
                if (is_successful) {
                    System.out.println("添加成功");
                    return;
                } else {
                    System.out.println("添加失败");
                    return;
                }
            }
        }
    }

    /**
     * 展示所有商品
     *
     * @param map Goods 集合
     */
    @Override
    public void showAll(LinkedHashMap<Integer, Goods> map) {
        GOODS_DAO.showAll(map);
    }

    /**
     * 根据id查询对应商品
     *
     * @param map Goods 集合
     */
    @Override
    public void selectById(LinkedHashMap<Integer, Goods> map) {
        while (true) {
            System.out.println("请输入商品编号：");
            Integer goodsId = INPUT.nextInt();
            //根据输入的商品编号调用Dao方法判断商品编号是否存在
            Goods goods = GOODS_DAO.selectById(map, goodsId);
            if (goods!=null) {
                //编号存在
                System.out.println("编号\t商品名称\t\t\t价格\t颜色\t尺寸\t库存");
                Set<Integer> keySet = map.keySet();
                for (Integer key : keySet) {
                    Goods value = map.get(key);
                    if (key.equals(goodsId)) {
                        //拿到集合中与用户输入的编号相同的信息并输出
                        System.out.println(key + "\t" + value.getGoodsName() + "\t\t\t" + value.getGoodsPrice() + "\t" + value.getGoodsColor() + "\t" + value.getGoodsSize() + "\t" + value.getStock());
                    }
                }
                break;
            } else {
                System.out.println("您输入的商品编号不存在");
//                continue;
            }
        }
    }

    /**
     * 根据id修改对应商品信息
     *
     * @param map Goods 集合
     */
    @Override
    public void updateById(LinkedHashMap<Integer, Goods> map) throws IOException {
        System.out.println("请输入您要修改的商品编号");
        Integer updateGoodsId = INPUT.nextInt();
        boolean is_exist = false;
        Set<Integer> keySet = map.keySet();
        for (Integer key : keySet) {
            if (key.equals(updateGoodsId)) {
                is_exist = true;
                break;
            }
        }
        if (is_exist) {
            //修改
            update(map, updateGoodsId);
        } else {
            System.out.println("您输入的商品编号不存在");
//            return;
        }
    }

    /**
     * 根据id删除商品
     *
     * @param map Goods集合
     */
    @Override
    public void deleteById(LinkedHashMap<Integer, Goods> map) throws IOException {
        System.out.println("请输入您要删除的商品id");
        Integer delId = INPUT.nextInt();
        Goods goods1 = GOODS_DAO.selectById(map, delId);
        if (goods1!=null) {
            System.out.println("是否确认删除：y/n");
            String confirm = INPUT.next();
            if ("y".equals(confirm)) {
                Goods goods = GOODS_DAO.deleteById(map, delId);
                System.out.println("删除成功！\n删除的商品信息如下");
                System.out.println("编号\t商品名称\t\t\t价格\t颜色\t尺寸\t库存");
                System.out.println(delId + "\t" + goods.getGoodsName() + "\t\t" + goods.getGoodsPrice() + "\t" + goods.getGoodsColor() + "\t" + goods.getGoodsSize() + "\t" + goods.getStock());
            }
        } else {
            System.out.println("您输入的商品编号不存在");
        }
    }


    //修改方法
    private void update(LinkedHashMap<Integer, Goods> map, Integer updateGoodsId) throws IOException {
        System.out.println("请输入修改后的商品名称：");
        String newGoodsName = INPUT.next();
        Double newGoodsPrice;
        while (true) {
            System.out.println("请输入修改后的商品价格：");
            newGoodsPrice = INPUT.nextDouble();
            if (newGoodsPrice < 0) {
                System.out.println("价格必须大于0");
//                continue;
            } else {
                break;
            }
        }
        System.out.println("请输入修改后的商品颜色：");
        String newGoodsColor = INPUT.next();
        System.out.println("请输入修改后的商品尺寸：");
        Double newGoodsSize = INPUT.nextDouble();
        int newGoodsStock;
        while (true) {
            System.out.println("请输入修改后的商品库存：");
            newGoodsStock = INPUT.nextInt();
            if (newGoodsStock < 0) {
                System.out.println("商品库存必须大于0");
//                continue;
            } else {
                break;
            }
        }
        System.out.println("是否确认修改：y/n");
        String confirm = INPUT.next();
        if ("y".equals(confirm)) {
            Goods newGoods = new Goods(newGoodsName, newGoodsPrice, newGoodsColor, newGoodsSize, newGoodsStock);
            boolean b = GOODS_DAO.updateById(map, updateGoodsId, newGoods);
            if (b) {
                System.out.println("修改成功");
            } else {
                System.out.println("修改失败");
            }
        }
    }
}